package com.fqgj.sentry.controller;

import com.alibaba.fastjson.JSON;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.fqgj.sentry.api.domain.RiskApiResult;
import com.fqgj.sentry.api.domain.request.DecisionVo;
import com.fqgj.sentry.biz.decision.DecisionBusiness;
import com.fqgj.sentry.biz.decision.DecisionBusinessV3;
import com.fqgj.sentry.common.redis.RedisUtil;
import com.fqgj.sentry.common.response.ApiOutput;
import com.fqgj.sentry.controller.request.EngineRequest;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by liupengpeng on 2017/8/10.
 */
@RequestMapping("/services/decision")
@RestController
public class DecisionController {
    private static Log logger = LogFactory.getLog(DecisionController.class);

    @Autowired
    private DecisionBusiness decisionBusiness;

    @Autowired
    private DecisionBusinessV3 decisionBusinessV3;


    @Autowired
    private RedisClient redisClient;

    /**
     * 风控决策
     *
     * @param addEventVo
     * @return
     */
    @RequestMapping(value = "/result", method = RequestMethod.POST)
    public ApiOutput decisionResult(@RequestBody DecisionVo addEventVo) {
        logger.info("decisionResult, addEventVo={}", JSON.toJSONString(addEventVo));
        RiskApiResult riskResult = decisionBusiness.decisionResult(addEventVo);
        logger.info("decisionResult, addEventVo={},  riskResult={}", JSON.toJSONString(addEventVo), JSON.toJSONString(riskResult));

        return new ApiOutput<>(riskResult);
    }

    @RequestMapping(value = "/columnCode/list", method = RequestMethod.POST)
    public ApiOutput decisionResultV2(@RequestBody EngineRequest request) {
        if (
                StringUtils.isEmpty(request.getMerchantCode()) ||
                        StringUtils.isEmpty(request.getAppCode()) ||
                        StringUtils.isEmpty(request.getEventCode())
        ) {
            logger.error("== 参数为空 ==");
            throw new ApplicationException("参数错误，不能为空");
        }


        String moduleKey = RedisUtil.getModuleKey(request.getMerchantCode(), request.getAppCode(), request.getEventCode());
        String modulesJsonStr = redisClient.getString(moduleKey);
        Set<String> modules = new HashSet<>();
        if (modulesJsonStr == null) {
            modules = decisionBusinessV3.getPolicySetsDataModules(request);
            redisClient.setString(moduleKey, JSON.toJSONString(modules));
            redisClient.expire(moduleKey,600);
        } else {
            List<Any> anies = JsonIterator.deserialize(modulesJsonStr).asList();
            for (Any a : anies) {
                if (null != a) {
                    modules.add(a.toString());
                }
            }
        }

        return new ApiOutput<>(modules);
    }
}
